package com.healthcenter.persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.healthcenter.entity.Patient;
import com.healthcenter.persistence.ConnectionFactory;
import com.healthcenter.persistence.dao.PatientDAO;

public class PatientDAOImpl implements PatientDAO {

	Connection con=null;
	  PreparedStatement ptmt=null;
	  ResultSet rs=null;
	  
	  public PatientDAOImpl()
	  {
	   
	  }
	  
	  private Connection getConnection() throws SQLException
	  {
	   Connection conn;
	   conn=ConnectionFactory.getInstance().getConnection();
	   return conn;
	  }
	  public void add(Patient patient)
	  {
	   
	   try
	   {
	    String querystring="INSERT INTO patients (first_name, last_name, id_card) VALUES(?,?,?)";
	    con=getConnection();
	    ptmt=con.prepareStatement(querystring);
	    ptmt.setString(1, patient.getName());
	    ptmt.setString(2, patient.getLastName());
	    ptmt.setString(3, patient.getIdCard());
	    ptmt.executeUpdate();
	    
	   }
	   catch(SQLException e)
	   {
	    e.printStackTrace();
	   }
	   finally
	   {
	    try
	    {
	    if(rs!=null)
	        rs.close();
	    if(ptmt!=null)
	       ptmt.close();
	    if(con!=null)
	       con.close();
	    }
	    catch(SQLException e)
	    {
	     e.printStackTrace();
	    }
	    catch(Exception e)
	    {
	     e.printStackTrace();
	    }
	    
	   }
	   
	  }
}
